<?php
/*
 *author:Yangligao
 *Date:2013-9-30
 */
require '../../Config.php';

$flag = $_GET['flag'] ;
$proModel = new FoodModel();
if($flag == 'display'){
	
	$where="";
	foreach ($_GET as $key=>$v){
		if($v !=""){
			if($key == "food_name"){
				$where .=" and {$key} like '%{$v}%'";
			}elseif ($key == "pricemin"){
				$where .=" and food_singleprice>=$v";
			}elseif ($key == "pricemax"){
				$where .=" and food_singleprice<=$v";
			}elseif ($key == "dated" && $v == 1){
				$where .= " and food_enddate < ".time();
			}elseif ($key == "dated" && $v == 0){
				$where .= " and food_enddate > ".time();	
			}elseif ($key == "flag" || $key == "page"){
				$where .= "";		
			}else
				$where .=" and {$key}={$v}";
		}
	}
	$where = "where 1=1".$where;
	$resNum = $proModel->food_select("count(*) as num",$where);
	
	$pageSize = 10;
	$totalCount = $resNum[0]['num'];
	$pageModel = new Page($pageSize, $totalCount);
	$startPos = $pageModel->getStart();
	$pageStr = $pageModel->getPageStr();
	$smarty->assign('pageStr',$pageStr);
	
	$proData = $proModel->food_select("*",$where,"limit {$startPos},{$pageSize}");
	for ($i=0;$i<count($proData);$i++){
		
		if($proData[$i]['food_enddate'] < time()){
			
			$proData[$i]['endflag'] = 1;
		}else{
			
			$proData[$i]['endflag'] = 0;
		}
	}
	$smarty->assign('proData',$proData);
	
	//查出供应商的名字 id
	$joincpModel = new JoinCpModel();
	
	$cpData = $joincpModel->joincp_select("cp_name,cp_id");
	$smarty->assign('cpData',$cpData);
	
	$smarty->display('Admin/ProM/prom.html');

}else if($flag == 'prodelete'){
	unset($_GET['prodelete']);
	
	$res = $proModel->food_delete($_GET);

	if($res){
		jump('操作成功！', $_SERVER['HTTP_REFERER']);
	}else{
		alertInfo('操作失败！！', $_SERVER['HTTP_REFERER']);
	}
	
	
}else if($flag == 'delMore'){
	if(isset($_POST['each'])){
		$keyArr = $_POST['each'];
	
	}else{
		$keyArr="";
	}
	$res = $proModel->food_deleteMoreByKey($keyArr);
	if($res){
		jump("记录删除成功", $_SERVER['HTTP_REFERER']);
	}else{
		jump("记录删除失败", $_SERVER['HTTP_REFERER']);
	}
}
?>
